package com.xiaoq.distribution.repository.impl;

import com.xiaoq.distribution.entity.SettleHistoryEntity;
import com.xiaoq.distribution.repository.ISettleHistoryRepository;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.Sort;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by chenjh on 2018/7/11.
 */
@Repository
public class SettleHistoryRepositoryImpl implements ISettleHistoryRepository {

    @Autowired
    private Datastore datastore;

    @Override
    public void save(SettleHistoryEntity settleHistoryEntity) {
        if(settleHistoryEntity != null){
            datastore.save(settleHistoryEntity);
        }
    }

    @Override
    public List<SettleHistoryEntity> getSettleHistoryList(String shopId, String distributorId, Integer theLatestSettle, Long settleBeginTime, Long settleEndTime, PagingVo pagingVo) {
        Query<SettleHistoryEntity> query = datastore.createQuery(SettleHistoryEntity.class);
        if(StringUtils.isNotBlank(shopId)){
            query.filter("shopId", shopId);
        }
        if (theLatestSettle != null){
            query.filter("theLatestSettle", theLatestSettle);
        }
        if (StringUtils.isNotBlank(distributorId)){
            query.filter("distributorId", distributorId);
        }
        if(settleBeginTime != null){
            query.filter("orderSettleTime >=", settleBeginTime);
        }
        if(settleEndTime != null){
            query.filter("orderSettleTime <=", settleEndTime);
        }
        if(pagingVo != null){
            query.offset(pagingVo.getPageOffset()).limit(pagingVo.getPs());
        }
        query.order(Sort.descending("createdAt"));
        return query.asList();
    }

    @Override
    public long getSettleHistoryCount(String shopId, String distributorId, Integer theLatestSettle, Long settleBeginTime, Long settleEndTime, PagingVo pagingVo) {
        Query<SettleHistoryEntity> query = datastore.createQuery(SettleHistoryEntity.class);
        if(StringUtils.isNotBlank(shopId)){
            query.filter("shopId", shopId);
        }
        if (theLatestSettle != null){
            query.filter("theLatestSettle", theLatestSettle);
        }
        if (StringUtils.isNotBlank(distributorId)){
            query.filter("distributorId", distributorId);
        }
        if(settleBeginTime != null){
            query.filter("orderSettleTime >=", settleBeginTime);
        }
        if(settleEndTime != null){
            query.filter("orderSettleTime <=", settleEndTime);
        }
        return query.count();
    }

    @Override
    public List<SettleHistoryEntity> getSettleHistoryListWithDistriIdList(String shopId, List<String> distributorIdList, Integer theLatestSettle) {
        Query<SettleHistoryEntity> query = datastore.createQuery(SettleHistoryEntity.class);
        if(StringUtils.isNotBlank(shopId)){
            query.filter("shopId", shopId);
        }
        if (theLatestSettle != null){
            query.filter("theLatestSettle", theLatestSettle);
        }
        if (distributorIdList != null){
            query.filter("distributorId in", distributorIdList);
        }
        query.order(Sort.descending("createdAt"));
        return query.asList();
    }

    @Override
    public SettleHistoryEntity getByDistributorId(String distributorId, Integer theLatestSettle) {
        Query<SettleHistoryEntity> query = datastore.createQuery(SettleHistoryEntity.class);
        if(StringUtils.isNotBlank(distributorId)){
            query.filter("distributorId", distributorId);
        }
        if (theLatestSettle != null){
            query.filter("theLatestSettle", theLatestSettle);
        }

        return query.get();
    }

    @Override
    public boolean updateById(SettleHistoryEntity settleHistoryEntity) {
        if(settleHistoryEntity == null || StringUtils.isBlank(settleHistoryEntity.getId())){
            return false;
        }

        Query<SettleHistoryEntity> query = datastore.createQuery(SettleHistoryEntity.class)
                .filter("_id", settleHistoryEntity.getId());
        UpdateOperations<SettleHistoryEntity> update = datastore.createUpdateOperations(SettleHistoryEntity.class);
        update.set("updatedAt", System.currentTimeMillis());

        if (settleHistoryEntity.getTheLatestSettle() != null){
            update.set("theLatestSettle", settleHistoryEntity.getTheLatestSettle());
        }
        datastore.update(query, update);
        return true;
    }

    @Override
    public SettleHistoryEntity getById(String id) {
        Query<SettleHistoryEntity> query = datastore.createQuery(SettleHistoryEntity.class)
                .filter("_id", id);
        return query.get();
    }
}
